/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AltaSustratoGrupo.java
 *
 * Created on 31/10/2011, 02:48:01 AM
 */
package vista.panel.altas;

import controlador.CargaPanel;
import controlador.ControlEventos;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import modelo.Grupo;
import modelo.SustratoNave;

/**
 *
 * @author Elias Mendoza
 */
public class AltaSustratoGrupo extends javax.swing.JPanel {
  SustratoNave sn;
  DefaultListModel modeloGrupoDisponible = new DefaultListModel();
  DefaultListModel modeloGrupoSeleccionado = new DefaultListModel();
  ControlEventos ce;
  CargaPanel cargaPanel;

  /** Creates new form AltaSustratoGrupo */
  public AltaSustratoGrupo(CargaPanel cargaPanel,SustratoNave sn) {
    this.sn = sn;
    this.cargaPanel=cargaPanel;
    initComponents();
    grupoDisponible.setModel(modeloGrupoDisponible);
    grupoSeleccionado.setModel(modeloGrupoSeleccionado);
    ce = new ControlEventos();
    ce.llenaComponenteGrupo(modeloGrupoDisponible);
    llenaEtiqueta();
  }

  /** This method is called from within the constructor to
   * initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is
   * always regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        grupoSeleccionado = new javax.swing.JList();
        jPanel3 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        jSplitPane1 = new javax.swing.JSplitPane();
        jLabel1 = new javax.swing.JLabel();
        nombreSustrato = new javax.swing.JLabel();
        jSplitPane2 = new javax.swing.JSplitPane();
        jLabel2 = new javax.swing.JLabel();
        constitucion = new javax.swing.JLabel();
        jSplitPane3 = new javax.swing.JSplitPane();
        jLabel3 = new javax.swing.JLabel();
        descripcion = new javax.swing.JLabel();
        jSplitPane4 = new javax.swing.JSplitPane();
        jLabel4 = new javax.swing.JLabel();
        referencia = new javax.swing.JLabel();
        jPanel5 = new javax.swing.JPanel();
        siguiente = new javax.swing.JButton();
        cancelar = new javax.swing.JButton();
        jPanel6 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        grupoDisponible = new javax.swing.JList();

        setBackground(new java.awt.Color(255, 255, 255));
        setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Registrar Grupos de Sensores con Sustrato", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Maiandra GD", 1, 14))); // NOI18N

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        jPanel2.setLayout(new java.awt.GridLayout(1, 0));

        grupoSeleccionado.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Selección", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.BELOW_TOP));
        grupoSeleccionado.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        grupoSeleccionado.setLayoutOrientation(javax.swing.JList.HORIZONTAL_WRAP);
        grupoSeleccionado.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                grupoSeleccionadoMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(grupoSeleccionado);

        jPanel2.add(jScrollPane1);

        jPanel3.setLayout(new java.awt.GridLayout(2, 0));

        jPanel4.setBackground(new java.awt.Color(255, 255, 255));
        jPanel4.setLayout(new java.awt.GridLayout(4, 0));

        jSplitPane1.setBackground(new java.awt.Color(255, 255, 255));
        jSplitPane1.setBorder(null);
        jSplitPane1.setDividerSize(3);
        jSplitPane1.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);

        jLabel1.setFont(new java.awt.Font("Times New Roman", 1, 14));
        jLabel1.setForeground(new java.awt.Color(51, 0, 153));
        jLabel1.setText("Sustrato:");
        jSplitPane1.setLeftComponent(jLabel1);

        nombreSustrato.setFont(new java.awt.Font("Maiandra GD", 1, 12));
        nombreSustrato.setText("jLabel5");
        nombreSustrato.setAutoscrolls(true);
        jSplitPane1.setRightComponent(nombreSustrato);

        jPanel4.add(jSplitPane1);

        jSplitPane2.setBackground(new java.awt.Color(255, 255, 255));
        jSplitPane2.setBorder(null);
        jSplitPane2.setDividerSize(3);
        jSplitPane2.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);

        jLabel2.setFont(new java.awt.Font("Times New Roman", 1, 14));
        jLabel2.setForeground(new java.awt.Color(51, 0, 153));
        jLabel2.setText("Constitucion:");
        jLabel2.setAutoscrolls(true);
        jSplitPane2.setLeftComponent(jLabel2);

        constitucion.setFont(new java.awt.Font("Maiandra GD", 1, 12));
        constitucion.setText("jLabel6");
        jSplitPane2.setRightComponent(constitucion);

        jPanel4.add(jSplitPane2);

        jSplitPane3.setBackground(new java.awt.Color(255, 255, 255));
        jSplitPane3.setBorder(null);
        jSplitPane3.setDividerSize(3);
        jSplitPane3.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);

        jLabel3.setFont(new java.awt.Font("Times New Roman", 1, 14));
        jLabel3.setForeground(new java.awt.Color(51, 0, 153));
        jLabel3.setText("Descripción:");
        jSplitPane3.setLeftComponent(jLabel3);

        descripcion.setFont(new java.awt.Font("Maiandra GD", 1, 12));
        descripcion.setText("jLabel7");
        jSplitPane3.setRightComponent(descripcion);

        jPanel4.add(jSplitPane3);

        jSplitPane4.setBackground(new java.awt.Color(255, 255, 255));
        jSplitPane4.setBorder(null);
        jSplitPane4.setDividerSize(3);
        jSplitPane4.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);

        jLabel4.setFont(new java.awt.Font("Times New Roman", 1, 14));
        jLabel4.setForeground(new java.awt.Color(51, 0, 153));
        jLabel4.setText("Referencia:");
        jSplitPane4.setLeftComponent(jLabel4);

        referencia.setFont(new java.awt.Font("Maiandra GD", 1, 12));
        referencia.setText("jLabel5");
        jSplitPane4.setRightComponent(referencia);

        jPanel4.add(jSplitPane4);

        jPanel3.add(jPanel4);

        jPanel5.setBackground(new java.awt.Color(255, 255, 255));
        jPanel5.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jPanel5.setLayout(new java.awt.GridBagLayout());

        siguiente.setIcon(new javax.swing.ImageIcon(getClass().getResource("/vista/img/siguiente.png"))); // NOI18N
        siguiente.setBorder(null);
        siguiente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                siguienteActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(19, 23, 0, 108);
        jPanel5.add(siguiente, gridBagConstraints);

        cancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/vista/img/cancelar.png"))); // NOI18N
        cancelar.setBorder(null);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(11, 27, 91, 108);
        jPanel5.add(cancelar, gridBagConstraints);

        jPanel3.add(jPanel5);

        jPanel6.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        jPanel6.setLayout(new java.awt.GridLayout(1, 0));

        grupoDisponible.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Disponible", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.BELOW_TOP));
        grupoDisponible.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        grupoDisponible.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        grupoDisponible.setLayoutOrientation(javax.swing.JList.VERTICAL_WRAP);
        grupoDisponible.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                grupoDisponibleMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(grupoDisponible);

        jPanel6.add(jScrollPane2);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, 230, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(18, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel6, javax.swing.GroupLayout.DEFAULT_SIZE, 316, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 316, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel3, 0, 0, Short.MAX_VALUE))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(19, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(33, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

  private void grupoSeleccionadoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_grupoSeleccionadoMouseClicked
    // TODO add your handling code here:
    if (evt.getClickCount() == 2) {
   
      //modeloGrupoDisponible.addElement(grupoSeleccionado.getModel().getElementAt(grupoSeleccionado.getSelectedIndex()));
      modeloGrupoDisponible.addElement(grupoSeleccionado.getModel().getElementAt(grupoSeleccionado.getSelectedIndex()));
      modeloGrupoSeleccionado.remove(grupoSeleccionado.getSelectedIndex());
    }
  }//GEN-LAST:event_grupoSeleccionadoMouseClicked

  private void grupoDisponibleMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_grupoDisponibleMouseClicked
    // TODO add your handling code here:
    
     if (evt.getClickCount() == 2) {
      modeloGrupoSeleccionado.addElement(grupoDisponible.getModel().getElementAt(grupoDisponible.getSelectedIndex()));       
      modeloGrupoDisponible.remove(grupoDisponible.getSelectedIndex());
     }
  }//GEN-LAST:event_grupoDisponibleMouseClicked

  private void siguienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_siguienteActionPerformed
    // TODO add your handling code here:
    
    Object [] grupos = modeloGrupoSeleccionado.toArray();
    int [] gruposAux = new int[grupos.length];
    for(int i=0;i<grupos.length;i++){
      gruposAux[i] = (int)grupos[i].toString().charAt(0);
      System.out.println(gruposAux[i]);
    }
    cargaPanel.cargaPanelNumeroSensoresGrupo(sn, gruposAux);
    
    
//    JOptionPane.showMessageDialog(null, "Esta a punto de Registrar: "+grupoSeleccionado.getSize()+" Grupos, con el Sustrato "+nombreSustrato.getText(), "Registro", JOptionPane.INFORMATION_MESSAGE);
//    ce.inserta(sn.getSustratoFK());
//   
//    for(int i =0; i<modeloGrupoSeleccionado.size();i++){
//      Grupo g = new Grupo();
//      g.setGrupo(modeloGrupoSeleccionado.getElementAt(i).toString().charAt(0));
//      System.out.println(g.getGrupo());
//      g.setSustrato(sn.getSustratoFK());
//      ce.inserta(g);
//    }
    
  }//GEN-LAST:event_siguienteActionPerformed

  public void llenaEtiqueta(){
    nombreSustrato.setText(sn.getSustratoFK().getNombre());
    constitucion.setText(sn.getSustratoFK().getConstitucion());
    descripcion.setText(sn.getSustratoFK().getDescripcion());
    referencia.setText(sn.getSustratoFK().getReferenciaSustrato().toString());
  }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelar;
    private javax.swing.JLabel constitucion;
    private javax.swing.JLabel descripcion;
    private javax.swing.JList grupoDisponible;
    private javax.swing.JList grupoSeleccionado;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JSplitPane jSplitPane2;
    private javax.swing.JSplitPane jSplitPane3;
    private javax.swing.JSplitPane jSplitPane4;
    private javax.swing.JLabel nombreSustrato;
    private javax.swing.JLabel referencia;
    private javax.swing.JButton siguiente;
    // End of variables declaration//GEN-END:variables
}
